package com.gams.springboot.controller;

import com.gams.springboot.services.MenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;


/**
 * @author tql
 * @current springboot
 * @create 2021-09-2-9:58
 */
@Controller
public class HelloController {
    @Autowired
    private MenuService menuService;

    private static final Logger LOGGER = LoggerFactory.getLogger(HelloController.class);

    @RequestMapping("/hello")
    public String hello(){
        return "hello world";
    }


    @RequestMapping("/index")
    public ModelAndView tree() {
        return new ModelAndView("/index.html");
    }

    @RequestMapping("/select")
    public ModelAndView select() {
        return new ModelAndView("/page/select.html");
    }



    @RequestMapping("/upload")
    public ModelAndView upload() {
        return new ModelAndView("/page/file.html");
    }

    @RequestMapping("/uploadSubmit")
    @ResponseBody
    public String uploadSubmit(@RequestParam("file") MultipartFile file){
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }

        String fileName = file.getOriginalFilename();
        String filePath = "/Users/";
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            LOGGER.info("上传成功");
            return "上传成功";
        } catch (IOException e) {
            LOGGER.error(e.toString(), e);
        }
        return "上传失败！";
    }
}

